Skip to content

IsValidEmail() Improved Regex Pattern #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

Huntk23
Copy link

@Huntk23 Huntk23 commented Dec 17, 2019

Made a slight update to the Regex Pattern to conform more to the rules of RFC 5322 which obsoletes RFC 2822.

I ended up updating the regex pattern because the built in .NET MailAddressParser conforms to RC2822 and did not want to re-implement to conform to RC5322. The supplied regex pattern should match ~99% of valid emails.

More tests have been implemented.

Old regex pattern:

--- Valid Email List ---
True : email@example.com
True : firstname.lastname@example.com
True : email@subdomain.example.com
False : firstname+lastname@example.com
True : email@123.123.123.123
True : email@[123.123.123.123]
True : 1234567890@example.com
True : email@example-one.com
True : _______@example.com
True : email@example.name
True : email@example.museum
True : email@example.co.jp
True : firstname-lastname@example.com

--- Invalid Email List ---
False : plainaddress
False : #@%^%#$@#$@#.com
False : @example.com
False : Joe Smith <email@example.com>
False : email.example.com
False : email@example@example.com
True : .email@example.com
True : email.@example.com
True : email..email@example.com
False : email@example.com (Joe Smith)
False : email@example
True : email@-example.com
False : email@example..com
True : Abc..123@example.com

New regex pattern:

--- Valid Email List ---
True : email@example.com
True : firstname.lastname@example.com
True : email@subdomain.example.com
True : firstname+lastname@example.com
True : email@123.123.123.123
True : email@[123.123.123.123]
True : 1234567890@example.com
True : email@example-one.com
True : _______@example.com
True : email@example.name
True : email@example.museum
True : email@example.co.jp
True : firstname-lastname@example.com

--- Invalid Email List ---
False : plainaddress
False : #@%^%#$@#$@#.com
False : @example.com
False : Joe Smith <email@example.com>
False : email.example.com
False : email@example@example.com
False : .email@example.com
False : email.@example.com
False : email..email@example.com
False : email@example.com (Joe Smith)
False : email@example
False : email@-example.com
False : email@example..com
False : Abc..123@example.com

For curiosities sake;

MailAddress constructor try/catch:

--- Valid Email List ---
True : email@example.com
True : firstname.lastname@example.com
True : email@subdomain.example.com
True : firstname+lastname@example.com
True : email@123.123.123.123
True : email@[123.123.123.123]
True : 1234567890@example.com
True : email@example-one.com
True : _______@example.com
True : email@example.name
True : email@example.museum
True : email@example.co.jp
True : firstname-lastname@example.com

--- Invalid Email List ---
False : plainaddress
False : #@%^%#$@#$@#.com
False : @example.com
True: Joe Smith <email@example.com>
False : email.example.com
False : email@example@example.com
False : .email@example.com
True : email.@example.com
True : email..email@example.com
True : email@example.com (Joe Smith)
True : email@example
True : email@-example.com
True : email@example..com
True : Abc..123@example.com

Fixes #27

@Huntk23
Copy link
Author

Huntk23 commented May 19, 2025

Crazy to think that it's been 6+ years since this pull request. This implementation can be improved upon by compiling the Regex statement since it should never change.

Is this library not accepting PRs?

@JonathanMagnan
Copy link
Member

Hello @Huntk23 ,

Honestly, we don't want to make any more changes to this library.

It works like it is now.

If we make some changes, we will probably just redo this library fully with extension members from C#14, but I doubt it will happen.

Best Regards,

Jon

@Huntk23 Huntk23 closed this May 19, 2025
@Huntk23
Copy link
Author

Huntk23 commented May 19, 2025

Understood, thanks for the response. Closing for now to clean up the repository stats.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

.IsValidEmail() reporting incorrect results
2 participants